package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.util.view.UniversalPhotoPop;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.CommodityManagementModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MerchantsTypeModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.UniversalModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/2.
 * 添加洗车 商品
 */

public class CarAddGoodsActivity extends BaseActivity implements View.OnClickListener, Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.categories_layout)//选择分类
            LinearLayout categories_layout;
    @Bind(R.id.categories)//分类
            TextView categories;

    @Bind(R.id.product_introduction)//简介
            EditText product_introduction;

    @Bind(R.id.five_original)//五座轿车 原价
            EditText five_original;

    @Bind(R.id.five_present)//五座轿车 现价
            EditText five_present;

    @Bind(R.id.suv_mpv_original)//suv_mpv 原价
            EditText suv_mpv_original;
    @Bind(R.id.suv_mpv_present)//suv_mpv 现价
            EditText suv_mpv_present;

    @Bind(R.id.taxi_original)//的士 原价
            EditText taxi_original;
    @Bind(R.id.taxi_present)//的士 现价
            EditText taxi_present;

    @Bind(R.id.button)//提交
            Button button;

    @Bind(R.id.title_center)
            TextView title_center;
    Context context;
    String pos = "0"; //0、添加商品  1、修改商品 2、展示商品
    String u_id = "";//商家ID  pos=0时 u_id 为店铺id   pos=1时 u_id 为商品id  pos=2时 u_id 为商品id
    String merchanttype = "";//类型
    String typeid = "";//类型id

    private MyProgressDialog mDialog;
    String Judge_interface = "0";//判断哪个接口   0、获取类型 1、上传
    int Judge_img = 0;// 类型是否点击
    List<UniversalModel> getclasstype = new ArrayList<>();//分类
    boolean Judge = false;
    private View viewkre;
    List<CommodityManagementModel> buycarlist = new ArrayList<>();
    int ints = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewkre = View.inflate(this, R.layout.activity_car_add_goods, null);
        setContentView(viewkre);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        categories_layout.setOnClickListener(this);
        button.setOnClickListener(this);
        MineFragmentModel mineFragmentModel = UserInfoUtils.readUserInfo(this);
        merchanttype = mineFragmentModel.getMerchanttype();
        if (getIntent() != null) {
            pos = getIntent().getStringExtra("pos");
            u_id = getIntent().getStringExtra("id");
            if (!pos.equals("0")) {
                ints = getIntent().getIntExtra("ints", 0);
                buycarlist = (List<CommodityManagementModel>) getIntent().getSerializableExtra("data");
            }
        }
        if (pos.equals("1")) {
            title_center.setText("修改商品");
           button.setText("修改");
            processdetails();
        } else if (pos.equals("2")) {
            title_center.setText("商品详情");
            button.setVisibility(View.GONE);
            processdetails();
            Judge_img = 1;
            processnNotEdit();
        }
        if (!pos.equals("2")) {
            processclasstype();
        }

    }

    //展示产品详情
    private void processdetails() {
        if (buycarlist != null|| buycarlist.size() > 0) {
            CommodityManagementModel ManagementModel = buycarlist.get(ints);
            categories.setText(ManagementModel.getNametype());
            product_introduction.setText(ManagementModel.getIntro());
            five_original.setText(ManagementModel.getLimousineprice());
            five_present.setText(ManagementModel.getLimousinediudiuprice());
            suv_mpv_original.setText(ManagementModel.getSuvprice());
            suv_mpv_present.setText(ManagementModel.getSuvdiudiuprice());
            taxi_original.setText(ManagementModel.getTaxiprice());
            taxi_present.setText(ManagementModel.getTaxidiudiuprice());
            typeid = ManagementModel.getClassid();
        }
    }

    //不可编辑
    public void processnNotEdit() {
        product_introduction.setEnabled(false);
        five_original.setEnabled(false);
        five_present.setEnabled(false);
        suv_mpv_original.setEnabled(false);
        suv_mpv_present.setEnabled(false);
        taxi_original.setEnabled(false);
        taxi_present.setEnabled(false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.categories_layout://类型选择
                if (Judge_img == 0) {
                    if (getclasstype.size() > 0) {
                        processtype();
                    } else {
                        Judge = true;
                        processclasstype();
                    }
                }
                break;
            case R.id.button://提交商品
                if (TextUtils.isEmpty(categories.getText().toString().trim())) {//商品分类
                    ToastUtils.show(context, getResources().getString(R.string.please_classification));
                    return;
                }
                if (TextUtils.isEmpty(product_introduction.getText().toString().trim())) {//商品简介
                    ToastUtils.show(context, getResources().getString(R.string.please_introduction));
                    return;
                }
                if (TextUtils.isEmpty(five_original.getText().toString().trim())) {//五座轿车原价
                    ToastUtils.show(context, getResources().getString(R.string.please_five_original));
                    return;
                }


                if (TextUtils.isEmpty(five_present.getText().toString().trim())) {//五座轿车现价
                    ToastUtils.show(context, getResources().getString(R.string.please_five_present));
                    return;
                }
                if (TextUtils.isEmpty(suv_mpv_original.getText().toString().trim())) {//suv_mpv原价
                    ToastUtils.show(context, getResources().getString(R.string.please_suv_mpv_original));
                    return;
                }
                if (TextUtils.isEmpty(suv_mpv_present.getText().toString().trim())) {//suv_mpv现价
                    ToastUtils.show(context, getResources().getString(R.string.please_suv_mpv_present));
                    return;
                }
                if (TextUtils.isEmpty(taxi_original.getText().toString().trim())) {//的士原价
                    ToastUtils.show(context, getResources().getString(R.string.please_taxi_original));
                    return;
                }
                if (TextUtils.isEmpty(taxi_present.getText().toString().trim())) {//的士现价
                    ToastUtils.show(context, getResources().getString(R.string.please_taxi_present));
                    return;
                }

                processupload();
                break;
        }
    }

    //绑定分类
    private void processtype() {
        UniversalPhotoPop typephoto = new UniversalPhotoPop(context, getclasstype, 1);
        typephoto.showAtLocation(viewkre, Gravity.BOTTOM, 0, 0);
        typephoto.setAddresskListener(new UniversalPhotoPop.OnAddressCListener() {
            @Override
            public void onClick(String province, String city, String area) {
                categories.setText(province);
                for (int i = 0; i < getclasstype.size(); i++) {
                    if (province.equals(getclasstype.get(i).getName())) {
                        typeid = getclasstype.get(i).getId();
                    }
                }
            }
        });
    }

    //商品分类
    private void processclasstype() {
        if (Sundry.isOpenNetwork(context)) {

            mDialog = new MyProgressDialog(context);
            Judge_interface = "0";
            HttpClient.classtype(Constant.CARWASHCLASSES, this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    //商品上传
    private void processupload() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            Judge_interface = "1";
            String data = "";

            if (pos.equals("0")) {//产品上传
                data = Constant.CARWASHUPLOAD;
            } else {
                data = Constant.CARWASHUPDATE;//产品修改
            }

//            String longitude = MyApplication.getInstance().getLongitude() + "";
//            String latitude = MyApplication.getInstance().getLatitude() + "";
//            String addrStr = MyApplication.getInstance().getAddrStr();
            HttpClient.CarWash(data, pos, u_id, typeid, product_introduction.getText().toString().trim(),
                    five_original.getText().toString().trim(), five_present.getText().toString().trim(), suv_mpv_original.getText().toString().trim(),
                    suv_mpv_present.getText().toString().trim(), taxi_original.getText().toString().trim(), taxi_present.getText().toString().trim() , this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }


    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result" + result);
        List<MerchantsTypeModel> getnote = JsonTools.getmerchanttype(result);
        if (getnote != null || getnote.size() > 0) {
            String error = getnote.get(0).getError();
            String mssage = getnote.get(0).getMssage();
            if (error.equals("200")) {
                String content = getnote.get(0).getResult();
                if (Judge_interface.equals("0")) {//商品分类
                    getclasstype = JsonTools.getclasstype(content);
                    if (Judge) {
                        processtype();
                    }
                } else if (Judge_interface.equals("1")) {//上传修改
                    finish();
                    ToastUtils.show(context, mssage);
                }
            } else if (error.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, mssage);
            } else {
                ToastUtils.show(context, mssage);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(Callback.CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}

